const path = require('path');
const webpack = require('webpack');
const HtmlWebPackPlugin = require('html-webpack-plugin');

//用于在构建前清除dist目录中的内容
const { CleanWebpackPlugin } = require('clean-webpack-plugin');

module.exports = {
    mode: "development",
    stats: { children: false },
    // Enable sourcemaps for debugging webpack's output.
    // devtool: "source-map",
    //关闭 webpack 的性能提示
    performance: {
        hints: false
    },

    resolve: {
        // Add '.ts' and '.tsx' as resolvable extensions.
        extensions: [".js", ".json", ".jsx", ".css", ".scss", ".ts", ".tsx"],
        alias: {
            '@src': path.join(__dirname, 'src/')
        }
    },

    entry: './docs/index.js', // 入口文件
    output: {
        path: path.resolve(__dirname, './docs/dist/'), // 定义输出目录
        filename: 'index.js'  // 定义输出文件名称
    },

    module: {
        rules: [
            {
                test: /\.js(x?)$/,
                use: 'babel-loader'
            },
            {
                test: /\.ts(x?)$/,
                exclude: /node_modules/,
                use: [
                    {
                        loader: "ts-loader"
                    }
                ]
            },
            {
                test: /\.css$/,
                use: [{
                    loader: "style-loader" // 将 JS 字符串生成为 style 节点
                }, {
                    loader: "css-loader" // 将 JS 字符串生成为 style 节点
                }]

            },
            {
                test: /\.scss$/,
                use: [
                    {
                        loader: "style-loader" // 将 JS 字符串生成为 style 节点
                    },
                    {
                        loader: "css-loader" // 将 CSS 转化成 CommonJS 模块
                    },
                    {
                        loader: "sass-loader" // 将 Sass 编译成 CSS
                    }
                ]
            },
            {
                enforce: "pre",
                test: /\.js$/,
                loader: "source-map-loader"
            },
            {
                test: /\.mdx?$/,
                use: ['babel-loader', '@mdx-js/loader']
            }
        ]
    },
    devServer: {
        hot: true, // 热替换
        contentBase: path.join(__dirname, './docs/dist'), // server文件的根目录
        compress: true, // 开启gzip
        port: 8082, // 端口
    },
    plugins: [
        // new webpack.HotModuleReplacementPlugin(), // HMR允许在运行时更新各种模块，而无需进行完全刷新
        // new CleanWebpackPlugin(),
        new HtmlWebPackPlugin({
            template: './docs/index.html',
            filename: path.resolve(__dirname, './docs/dist/index.html')
        })
    ],
    // When importing a module whose path matches one of the following, just
    // assume a corresponding global variable exists and use that instead.
    // This is important because it allows us to avoid bundling all of our
    // dependencies, which allows browsers to cache those libraries between builds.
    // externals: {
    //     "react": "React",
    //     "react-dom": "ReactDOM"
    // }
};